package com.it.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.aiReportClaim.ReportClaimApi;
import com.it.entity.adjustment.AiClaimCalculation;
import com.it.entity.aiClaimRegistration.AiClaimRegistration;
import com.it.mapper.AiClaimCalculationMapper;
import com.it.service.AiClaimCalculationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.R;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 保险金额理算表（立案后计算赔付金额） 服务实现类
 * </p>
 *
 * @author lutao
 * @since 2025年09月23日
 */
@Service
public class AiClaimCalculationServiceImpl extends ServiceImpl<AiClaimCalculationMapper, AiClaimCalculation> implements AiClaimCalculationService {

    @Autowired
    private AiClaimCalculationMapper aiClaimCalculationMapper;

    @Autowired
    private ReportClaimApi reportClaimApi;

    @Override
    public R addAdjustment(AiClaimCalculation aiClaimCalculation) {
        aiClaimCalculation.setCalculationTime(LocalDateTime.now());
        aiClaimCalculation.setCalculationStatus(1);
        reportClaimApi.updateStatus(aiClaimCalculation.getReportNo(),6);
        aiClaimCalculationMapper.insert(aiClaimCalculation);
        return R.Success("添加成功");
    }

    @Override
    public R CalculationList(Integer pageNum, Integer pageSize, Integer calculationStatus) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<AiClaimCalculation> queryWrapper = new LambdaQueryWrapper<>();
        if (calculationStatus != null) {
            queryWrapper.eq(AiClaimCalculation::getCalculationStatus, calculationStatus);
        }
        List<AiClaimCalculation> aiClaimCalculations = aiClaimCalculationMapper.selectList(queryWrapper);
        return R.Success("查询成功", new PageInfo<>(aiClaimCalculations));
    }

    @Override
    public R CalculationById(String calculationId) {
        LambdaQueryWrapper<AiClaimCalculation> queryWrapper = new LambdaQueryWrapper<>();
        if (calculationId != null) {
            queryWrapper.eq(AiClaimCalculation::getCalculationId, calculationId);
        }else {
            return R.Failed("请输入保单号");
        }
        AiClaimCalculation aiClaimCalculation = aiClaimCalculationMapper.selectOne(queryWrapper);
        return R.Success("查询成功",aiClaimCalculation);
    }

    @Override
    public R getByReportNo(String reportNo, Integer calculationStatus) {
        LambdaQueryWrapper<AiClaimCalculation> queryWrapper = new LambdaQueryWrapper<>();
        if (calculationStatus != null) {
            queryWrapper.eq(AiClaimCalculation::getCalculationStatus, calculationStatus);
        }
        if (reportNo != null) {
            queryWrapper.eq(AiClaimCalculation::getReportNo, reportNo);
        }
        AiClaimCalculation aiClaimCalculation = aiClaimCalculationMapper.selectOne(queryWrapper);
        if (aiClaimCalculation != null) {
            return R.Success("查询理算信息成功",aiClaimCalculation);
        }
        return R.Failed("无符合要求的理算信息");
    }
}
